<?php

namespace admin\controllers;

use admin\components\AdminController;
use app\models\User;

class CompanyController extends AdminController {

    public $name = '企业管理';

    public function actionIndex() {
        $searchModel = !$this->enableSearch ? NULL : new User();
        $query = User::find()->where(['type' => [User::TYPE_COMPANY, User::TYPE_ORG]])->with(['profile']);
        $query->orderBy('status desc, id desc');
        $dataProvider = $this->search($query, $searchModel);
        return $this->render(compact('searchModel', 'dataProvider'));
    }


    public function actionSave($id = FALSE) {
        /* @var $model User */
        $model = $this->findModel($id, User::className());
        if (!$model->load($this->post())) {
            return $this->render(compact('model'));
        }
        return $model->save();
    }

    public function actionView($id) {
        $model = $this->findModel($id, User::className());
        return $this->render(compact('model'));
    }

    public function actionAllow($id) {
        $model = User::findOne($id);
        if (!$model || !$model->getIsVerify()) {
            return $this->error();
        }

        $model->status = User::STATUS_ACTIVE;
        return $model->save(TRUE, ['status']);
    }

    public function actionDeny($id) {
        $model = User::findOne($id);
        if (!$model || !$model->getIsVerify()) {
            return $this->error();
        }

        $model->status = User::STATUS_VERIFY_DENY;
        $model->remark = $this->get('remark');
        return $model->save(TRUE, ['status', 'remark']);
    }
}
